import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class PortScanner {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的主机IP地址：");
        String host = scanner.nextLine();
        System.out.println("请输入要扫描的起始端口号：");
        int startPort = scanner.nextInt();
        System.out.println("请输入要扫描的结束端口号：");
        int endPort = scanner.nextInt();

        for (int port = startPort; port <= endPort; port++) {
            if (isPortOpen(host, port)) {
                System.out.println("端口 " + port + " 是开放的");
            } else {
                System.out.println("端口 " + port + " 是关闭的");
            }
        }
    }

    private static boolean isPortOpen(String host, int port) {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), 100);
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}
